package com.video.web.controller.system;

import java.util.List;

import com.video.common.utils.MinioAPI;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.video.common.annotation.Log;
import com.video.common.enums.BusinessType;
import com.video.system.domain.VideoAdvertising;
import com.video.system.service.IVideoAdvertisingService;
import com.video.common.core.controller.BaseController;
import com.video.common.core.domain.AjaxResult;
import com.video.common.utils.poi.ExcelUtil;
import com.video.common.core.page.TableDataInfo;

/**
 * 广告Controller
 * 
 * @author lyh
 * @date 2021-07-21
 */
@Controller
@RequestMapping("/system/advertising")
public class VideoAdvertisingController extends BaseController
{
    private String prefix = "system/advertising";

    @Autowired
    private IVideoAdvertisingService videoAdvertisingService;
    @Autowired
    private MinioAPI minioAPI;


    /**
     * 获取上传图片的URL
     * @return
     * @throws Exception
     */
    @PostMapping("/show")
    @ResponseBody
    public AjaxResult showUrl(String fileName) throws Exception {
        //桶
        String bucket="test";

        // 获取文件签名url
        String signedUrl = minioAPI.getSignedUrl(bucket, fileName, 300);
        // 该路径可访问文件五分钟
        System.out.println("=========================>signedUrl:"+signedUrl);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("url", signedUrl);//存入上传路径
        return ajax;
    }

    @RequiresPermissions("system:advertising:view")
    @GetMapping()
    public String advertising()
    {
        return prefix + "/advertising";
    }

    /**
     * 查询广告列表
     */
    @RequiresPermissions("system:advertising:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VideoAdvertising videoAdvertising)
    {
        startPage();
        List<VideoAdvertising> list = videoAdvertisingService.selectVideoAdvertisingList(videoAdvertising);
        return getDataTable(list);
    }

    /**
     * 导出广告列表
     */
    @RequiresPermissions("system:advertising:export")
    @Log(title = "广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VideoAdvertising videoAdvertising)
    {
        List<VideoAdvertising> list = videoAdvertisingService.selectVideoAdvertisingList(videoAdvertising);
        ExcelUtil<VideoAdvertising> util = new ExcelUtil<VideoAdvertising>(VideoAdvertising.class);
        return util.exportExcel(list, "广告数据");
    }

    /**
     * 新增广告
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存广告
     */
    @RequiresPermissions("system:advertising:add")
    @Log(title = "广告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VideoAdvertising videoAdvertising)
    {
        return toAjax(videoAdvertisingService.insertVideoAdvertising(videoAdvertising));
    }

    /**
     * 修改广告
     */
    @GetMapping("/edit/{advertisingId}")
    public String edit(@PathVariable("advertisingId") Integer advertisingId, ModelMap mmap)
    {
        VideoAdvertising videoAdvertising = videoAdvertisingService.selectVideoAdvertisingById(advertisingId);
        mmap.put("videoAdvertising", videoAdvertising);
        return prefix + "/edit";
    }

    /**
     * 修改保存广告
     */
    @RequiresPermissions("system:advertising:edit")
    @Log(title = "广告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VideoAdvertising videoAdvertising)
    {
        return toAjax(videoAdvertisingService.updateVideoAdvertising(videoAdvertising));
    }

    /**
     * 删除广告
     */
    @RequiresPermissions("system:advertising:remove")
    @Log(title = "广告", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(videoAdvertisingService.deleteVideoAdvertisingByIds(ids));
    }
}
